/**
 * Advanced Software Engineering - assignment part 1 (monica Farrow)
 * Customer class
 * 
 * Some of the methods in this class have been based on lecture notes provided by
 * Monica Farrow as a part of Software Engineering courses at Heriot-Watt University.
 */

package depot;

import java.util.Collections;

/**
 * Advanced Software Engineering - F21AS.
 * Customer class with customer details.
 * @author Ionut Iordache, Nikky Afunugo, Dave Bond
 */
public class Customer {
	private String customerID;
	private String firstName;
	private String lastName;
	private Parcel custParcel;

	
	/** 
	 * constructor for Customer
	 * Creates an Customer object with values specified in the parameters (no middle name)
	 * @param id		the ID of the customer
	 * @param fName		the first name of the customer
	 * @param lName		the last name of the customer
	 * @param Parcel	the parcel of the customer
	 * @throws IllegalArgumentException
	 */
	public Customer(String id, String fName, String lName, Parcel pcl) {
		
		// check length of ID is 6
		if(id.length() != 6) {
			throw new IllegalArgumentException("CustomerID must contain 6 characters");			
		}
		
		// check ID contains only digits
		if(! id.matches("[0-9]*")) {
			throw new IllegalArgumentException("CustomerID must contain only digits");			
		}
		
		// check length of first name is between 2 and 15 characters
		if((fName.length() < 2) || (fName.length() > 15)) {
			throw new IllegalArgumentException("First name must contain 2-15 characters");
		}
		
		// check length of last name is between 2 and 15 characters
		if((lName.length() < 2) || (lName.length() > 15)) {
			throw new IllegalArgumentException("Last name must contain 2-15 characters");
		}
		
		// check first name contains only valid characters
		if(! fName.matches("[a-zA-Z'\\s-]*")) {
			throw new IllegalArgumentException("First name contains invalid characters");
		}
		
		// check last name contains only valid characters
		if(! lName.matches("[a-zA-Z'\\s-]*")) {
			throw new IllegalArgumentException("Last name contains invalid characters");
		}
		
		// check first name doesn't contains only spaces
		if(fName.matches("[\\s]*")) {
			throw new IllegalArgumentException("First name contains only spaces");
		}
		
		// check last name doesn't contains only spaces
		if(lName.matches("[\\s]*")) {
			throw new IllegalArgumentException("Last name contains only spaces");
		}
		
		customerID = id;
		firstName = fName;
		lastName = lName;
		custParcel = pcl;
	}

	
	/** 
	 * returns the ID of the customer
	 * @return the customer ID
	 */
	public String getCustID() {
		return customerID;
	}

	
	/** 
	 * returns the first name of the customer
	 * @return the first name
	 */
	public String getFirstName() {
		return firstName;
	}

	
	/**
	 * sets the first name to the value provided in the parameter
	 * @param fName   first name
	 */
	public void setFirstName(String fName) {

		fName = fName.trim();
		firstName = fName;
	} 

	
	/** 
	 * returns the last name of the customer
	 * @return the last name
	 */
	public String getLastName() {
		return lastName;
	}

	
	/**
	 * sets the first name to the value provided in the parameter
	 * @param fName   first name
	 */
	public void setLastName(String lName) {

		lName = lName.trim();
		lastName = lName;
	} 

	
	/** 
	 * returns the parcel of the customer
	 * @return the parcel
	 */
	public Parcel getParcel() {
		return custParcel;
	}

	
	/**
	 * sets the parcel to the value provided in the parameter
	 * @param pcl   Parcel name/ID
	 */
	public void setParcel(Parcel pcl) {
		custParcel = pcl;
	} 

	
	/** 
	 * returns the ID of the parcel
	 * @return the parcel ID
	 */
	public String getParcelID() {
		return custParcel.getParcelID();
	}

	
	/** 
	 * overrides equals methods higher up the hierarchy
	 * first check that other object being compared IS a Customer
	 * if so, cast to an Customer
	 * @return true if equal, false otherwise
	 */
	public boolean equals(Object other){
		if (!(other instanceof Customer) )
			return false;
		Customer otherCust = (Customer) other;
		return (this.customerID.equals(otherCust.customerID));
	}

	
	/**
	 *  toString method for Customer class
	 * @return  competitor's ID, name, and parcelID
	 */
	public String toString() {
		return "ID: " + this.getCustID() + ",  Name: " + this.getFirstName() + " " + this.getLastName() +",  ParcelID: " + this.getParcelID();
	}

	
}
